﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;

namespace Com.GUI.LibraryManager.Controls.SubControl
{
    /// <summary>
    /// Interaction logic for ucAirConditionWallGlass.xaml
    /// </summary>
    public partial class ucAirConditionWallGlass : UserControl
    {
        public ucAirConditionWallGlass()
        {
            InitializeComponent();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            loadData();
        }
        public void loadData()
        {
            cbxDescription.ItemsSource = LibraryManagerInCom.WoodGlassCoefficientList;
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;
        }
        public void reload()
        {
            cbxDescription.ItemsSource = null;
            cbxDescription.Items.Clear();
            foreach( var item in LibraryManagerInCom.WoodGlassCoefficientList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_wood_door_heat_coefficient wd = (tb_wood_door_heat_coefficient)cbxDescription.SelectedItem;
            if (wd != null)
            {
                if (wd.type == 1)
                {
                    rdWood.IsChecked = true;
                }
                else
                {
                    rdWall.IsChecked = true;
                }
                txtCharacteristic.Text = wd.characteristic;
                txtCoefficient.Text = wd.coefficient.ToString();
            }
        }
        public void newData()
        {
            cbxDescription.Text = "";
            rdWall.IsChecked = true;
            txtCharacteristic.Text = "";
            txtCoefficient.Text = "";
        }

        public void saveData()
        {
            tb_wood_door_heat_coefficient wd = new tb_wood_door_heat_coefficient();
            wd.description = cbxDescription.Text;
            if (rdWall.IsChecked == true)
            {
                wd.type = 1;
            }
            else
            {
                wd.type = 2;
            }
            wd.characteristic = txtCharacteristic.Text;
            wd.coefficient = Convert.ToDouble(txtCoefficient.Text);
            LibraryManagerInCom.addWoodDoorWallGlass(wd);
            reload();
        }
        public void delData()
        {
            tb_wood_door_heat_coefficient wd = (tb_wood_door_heat_coefficient)cbxDescription.SelectedItem;
            LibraryManagerInCom.delWoodDoorWallGlass(wd);
            reload();
        }
        public void copyData()
        {
            tb_wood_door_heat_coefficient wd = LibraryManagerInCom.copyWoodDoorWallGlass((tb_wood_door_heat_coefficient)cbxDescription.SelectedItem);
            cbxDescription.Text = wd.description;
            if (wd.type == 1)
            {
                rdWood.IsChecked = true;
            }
            else
            {
                rdWall.IsChecked = true;
            }
            txtCharacteristic.Text = wd.characteristic;
            txtCoefficient.Text = wd.coefficient.ToString();
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }
}
